package com.wj.controller;

import com.wj.domain.vo.Video;
import com.wj.tool.common.bean.restful.RestfulBody;
import com.wj.tool.utils.HtmlUtils;
import com.wj.tool.utils.JsoupUtil;
import io.swagger.annotations.Api;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/video")
@RestfulBody
@Api("获取视频地址")
public class WriteVideoController {

    @GetMapping("/jiexi")
    public List<Video> writeVideo(String url) throws Exception {
        WriteVideoController t = new WriteVideoController();
        List<Video> list = t.getData(url);
        return list;
    }


    public List<Video> getData (String url) throws Exception{
        //获取的数据，存放在集合中
        List<Video> data = new ArrayList<Video>();
        url = url.replace("\\", "");
        if (url.contains("https://v.qq.com/x/cover") || url.contains("https://m.v.qq.com/")){
            data = getTXData(url);
        }
        //返回数据
        return data;
    }

    // 获取腾讯视频
    public static List<Video> getTXData (String url) throws Exception{
        //获取的数据，存放在集合中
        List<Video> data = new ArrayList<Video>();
        url = url.replace("\\", "");
        if (url.contains("https://v.qq.com/x/cover")){
            String jiPreUrl = "https://v.qq.com/x/cover";
            //采用Jsoup解析
            Document doc = JsoupUtil.getDoc(url);

            Element element = doc.select("div.episode-list-rect__list").first();
            if (element != null){
                //获取html标签中的内容
                Elements elements=element.select("div.episode-list-rect__item");
                int i=1;
                System.out.println("begin get ele");
                for (Element ele:elements) {

                    String datavid = ele.select("div").attr("data-vid");
                    String datacid = ele.select("div").attr("data-cid");

                    String jiUrl = jiPreUrl + "/" + datacid + "/" + datavid + ".html";
                    String title = ele.select("div span").html();
                    Video video = new Video();
                    video.setId(title);
                    video.setTitle(title);
                    video.setUrl(jiUrl);
                    data.add(video);
                }
            }
        } else if (url.contains("https://m.v.qq.com/")){
            String jiPreUrl = "https://m.v.qq.com/";
            //采用Jsoup解析
            Document doc = JsoupUtil.getDoc(url);

            String rawHTML = HtmlUtils.getRawHTML(url);


            Element element = doc.select("div.mod_list_slider").first();
            if (element != null){
                //获取html标签中的内容
                Elements elements=element.select("li");
                int i=1;
                System.out.println("begin get ele");
                for (Element ele:elements) {

                    String sufUrl = ele.select("a").attr("href");
                    String title = ele.select("a").attr("title");
                    String imgAlt = ele.select("img").attr("alt");
                    String jiUrl = jiPreUrl + sufUrl;
                    Video video = new Video();
                    video.setId(title);
                    video.setTitle(title);
                    video.setUrl(jiUrl);
                    if (imgAlt.contains("预")){
                        video.setRemark("预");
                    }
                    data.add(video);
                }
            }
        }

        //返回数据
        return data;
    }

    // 获取爱奇艺视频
    public static List<Video> getIQYData (String url) throws Exception{
        //获取的数据，存放在集合中
        List<Video> data = new ArrayList<Video>();
        url = url.replace("\\", "");
        if (url.contains("https://v.qq.com/x/cover")){
            String jiPreUrl = "https://v.qq.com/x/cover";
            //采用Jsoup解析
            Document doc = JsoupUtil.getDoc(url);

            Element element = doc.select("div.episode-list-rect__list").first();
            if (element != null){
                //获取html标签中的内容
                Elements elements=element.select("div.episode-list-rect__item");
                int i=1;
                System.out.println("begin get ele");
                for (Element ele:elements) {

                    String datavid = ele.select("div").attr("data-vid");
                    String datacid = ele.select("div").attr("data-cid");

                    String jiUrl = jiPreUrl + "/" + datacid + "/" + datavid + ".html";
                    String title = ele.select("div span").html();
                    Video video = new Video();
                    video.setId(title);
                    video.setTitle(title);
                    video.setUrl(jiUrl);
                    data.add(video);
                }
            }
        }

        //返回数据
        return data;
    }


    public static void main(String[] args) throws Exception {
        WriteVideoController t = new WriteVideoController();
        String url = "https://v.qq.com/x/cover/mzc00200shcxcyx/l0043mccqpp.html?n_version=2021";
//        t.writeVideo("https://v.qq.com/x/cover/mzc00200p51jpn7/j0043h9kf6x.html?n_version=2021");
        List<Video> data = getTXData(url);
        System.out.println(data.toString());
    }
}